package yxy.game.sdk.oppo_quick;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.digest.DigestUtils;
import yxy.apple.http.server.business.BusinessType;
import yxy.apple.logger.Logger;
import yxy.apple.util.MD5Utility;
import yxy.game.pm2.bean.channel.SdkChannel;
import yxy.game.pm2.bean.db.sdk.SDK_UnionChannel;
import yxy.game.pm2.bean.db.sdk.SDK_UnionChannelDAO;
import yxy.game.util.HttpHelper;

import java.util.Iterator;
import java.util.Set;
import java.util.TreeMap;

//根据token获取用户信息
@BusinessType(code = "TOKEN")
public class Token extends Sdk_vivo_quick {
    private static final Logger logger = Logger.create(Token.class);

    @Override
    public JSONObject handle(JSONObject request) throws Exception {
        logger.info(SdkChannel.sdk_oppo_quick + ":token:%s", request.toJSONString());
        JSONObject result = new JSONObject();
        result.put("result", 0);
        result.put("message", "");

        try {
            String appId = request.getString("appId");
            String token = request.getString("token");
            SDK_UnionChannel unionChannel = SDK_UnionChannelDAO.getByAppId(SdkChannel.sdk_oppo_quick, appId);
            if (unionChannel == null) throw new Exception("渠道不存在");

            TreeMap<String, String> signMap = new TreeMap<>();
            signMap.put("pkgName", unionChannel.getPkgName());
            signMap.put("appKey", unionChannel.getAppKey());
            signMap.put("appSecret", unionChannel.getAppSecret());
            signMap.put("timestamp", String.valueOf(System.currentTimeMillis()));
            signMap.put("token", token);
            String content = makeSignContent(signMap); //加密串组合
            logger.info(SdkChannel.sdk_oppo_quick + ":content:" + content);
            String sign = MD5Utility.MD5(content).toUpperCase(); //生成sign
            logger.info(SdkChannel.sdk_oppo_quick + ":sign:" + sign);

            JSONObject params = new JSONObject();
            params.put("pkgName", unionChannel.getPkgName());
            params.put("token", token);
            params.put("timestamp", signMap.get("timestamp"));
            params.put("sign", sign);
            params.put("version", "1.0.0");

            StringBuilder builder = new StringBuilder();
            params.forEach((k, v) -> {
                if (builder.length() > 0) builder.append("&");
                builder.append(k).append("=").append(v);
            });
            String url = "https://play.open.oppomobile.com/instant-game-open/userInfo?" + builder.toString();
            logger.info(SdkChannel.sdk_oppo_quick + ":url:" + url);
            String res = HttpHelper.doGet(url); //
            logger.info(SdkChannel.sdk_oppo_quick + ":url:" + res);
            JSONObject resobj = JSONObject.parseObject(res);
            if (!resobj.getString("errCode").equals("200")) throw new Exception(resobj.getString("errMsg"));
            result.put("userInfo", resobj.get("userInfo"));
        } catch (Exception e) {
            result.put("result", 99);
            result.put("message", "token验证失败:" + e.getMessage());
        }
        return result;
    }

    //生成签名串
    private String makeSignContent (TreeMap<String, String> signMap) {

        Set<String> keys = signMap.keySet();

        StringBuilder signString = new StringBuilder();
        Iterator<String> iterator = keys.iterator();
        while (iterator.hasNext()) {
            String key = iterator.next();
            signString.append(key).append("=").append(signMap.get(key)).append("&");
        }
        String signatureContent = signString.toString().substring(0, signString.length() - 1);
        return signatureContent;
    }
}
